package dkzcm.club.article.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import dkzcm.club.article.application.convert.ArticleCategoryDTOConverter;
import dkzcm.club.article.application.dto.ArticleCategoryDTO;
import dkzcm.club.article.common.entity.Result;
import dkzcm.club.article.domain.entity.ArticleCategoryBO;
import dkzcm.club.article.domain.service.ArticleCategoryDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @desc:
 * @author: xk
 * @version: 1.0
 * Create by 2024/11/10 15:38
 */
@RequestMapping("/article/category")
@RestController
@Slf4j
public class ArticleCategoryController {

    @Resource
    private ArticleCategoryDomainService articleCategoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody ArticleCategoryDTO articleCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("article add controller:{}", JSON.toJSONString(articleCategoryDTO));
            }

            Preconditions.checkNotNull(articleCategoryDTO.getCategoryName(), "分类名称不能为空");
//            Preconditions.checkArgument(!StringUtils.isBlank(articleCategoryDTO.getImgUrl()),"imgul不能为空");
//            Preconditions.checkArgument(StringUtils.isEmpty(articleCategoryDTO.getCategoryName()),"分类名称不能为空");
            ArticleCategoryBO articleCategoryBO = ArticleCategoryDTOConverter.INSTANCE.convertDtoToCategoryBO(articleCategoryDTO);
            articleCategoryDomainService.add(articleCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            System.out.println("ArticleCategoryController add error:{}" + e);
            return Result.fail(e.getMessage());
        }
    }


    @PostMapping("/queryByPrimary")
    public Result<List<ArticleCategoryDTO>> queryPrimaryCategory(ArticleCategoryDTO articleCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("article add controller:{}", JSON.toJSONString(articleCategoryDTO));
            }
            List<ArticleCategoryBO> articleCategoryBOList = articleCategoryDomainService.queryPrimaryCategory();
            List<ArticleCategoryDTO> articleCategoryDTOList = ArticleCategoryDTOConverter.INSTANCE.convertCategoryBOsToDTOs(articleCategoryBOList);
            return Result.ok(articleCategoryDTOList);
        } catch (Exception e) {
            log.error("ArticleCategoryController queryPrimaryCategory error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }

    }

}
